/**
 * 
 */
package org.vsg.stock.basic.algorithm;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.vsg.stock.basic.DailyStock;

/**
 * @author Bill Vison
 *
 */
public class AnalysisIndex {

	private List<DailyStock> allEntity;
	
	private Map<Date , DailyStock> mapAllEntiy;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private DecimalFormat nf = new DecimalFormat("##0.000");	
	
	public AnalysisIndex(List<DailyStock> allEntity) {
		
		this.allEntity = allEntity;
		
		mapAllEntiy = new HashMap<Date , DailyStock>();
		
		DailyStock stock = null;
		for(Iterator<DailyStock> iterEnt = allEntity.iterator(); iterEnt.hasNext();) {
			stock = iterEnt.next();
			mapAllEntiy.put( stock.getDate() , stock);
		}
		
	}
	
	public  double medianPoint(Date specDate , int cycle) {
		
		DailyStock ds = mapAllEntiy.get( specDate );
		int position = allEntity.indexOf(ds);
		
		double sumMidPrice = 0;
		DailyStock stock = null;
		
		if (position-cycle >= 0) {
			
			for (int i = position-cycle+1 ; i <= position ; i++) {
				stock = allEntity.get(i);
				double midPrice = ( stock.getOpen() + stock.getClose() )/ 2;
				sumMidPrice += midPrice;
			}
			
		}

		
		return Double.parseDouble( nf.format( sumMidPrice / cycle) );
	}

}
